package Dao;
import java.util.List;
import java.io.*;
import java.util.ArrayList;
import entity.User;

public class UserDaoimpl implements UserDao {
    /*
    实现添加用户
     */
    public boolean insertUser(User user) {
        try {
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("QQSystem\\User.txt", true));
            bufferedWriter.write(user.getId() + "," + user.getUserName() + "," + user.getPassWord() + "," + user.getQuestion() + "," + user.getAnswer() + "," + user.getCall());
            bufferedWriter.newLine();
            bufferedWriter.close();
            return true;
        } catch (Exception e) {
            System.out.println("io异常，添加失败！");
            return false;
        }
    }
    /*
    删除操作
     */
    @Override
    public boolean deleteUserById(Integer id) {
        try{
            BufferedReader bufferedReader=new BufferedReader(new FileReader("QQSystem\\User.txt"));
            List<User> userList=new ArrayList<>();
            String string;
            while((string=bufferedReader.readLine())!=null){
                String[] dates=string.split(",");
                User user=new User(Integer.parseInt(dates[0]),dates[1],dates[2],dates[3],dates[4],Integer.parseInt(dates[5]));
                userList.add(user);
            }
            for (int i = 0; i < userList.size(); i++) {
                User user = userList.get(i);
                if ((user.getId()).equals(id)) {
                    userList.remove(i);
                }
            }
            BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("QQSystem\\User.txt"));
            for (int i = 0; i < userList.size(); i++) {
                User user=userList.get(i);
                String returnString =String.format("%s,%s,%s,%s,%s,%s",user.getId(),user.getUserName(),user.getPassWord(),user.getQuestion(),user.getAnswer(),user.getCall());
                bufferedWriter.write(returnString);
                bufferedWriter.newLine();
            }
            bufferedReader.close();
            bufferedWriter.close();
            return true;
        }
        catch (IOException e){
            System.out.println("io异常,删除失败！");
            return  false;
        }
    }
    /*
    更改用户信息
     */
    @Override
    public boolean changeUserInformationById(Integer id, User user) {
        try{
            BufferedReader bufferedReader=new BufferedReader(new FileReader("QQSystem\\User.txt"));
            List<User> userList=new ArrayList<>();
            String string;
            while ((string=bufferedReader.readLine())!=null){
                String[] dates=string.split(",");
                User user1=new User(Integer.parseInt(dates[0]),dates[1],dates[2],dates[3],dates[4],Integer.parseInt(dates[5]));
                userList.add(user1);
            }
            for (int i = 0; i < userList.size(); i++) {
                User user1= userList.get(i);
                if (user1.getId().equals(id)){
                    userList.set(i,user);
                }
            }
            BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("QQSystem\\User.txt"));
            for (int i = 0; i < userList.size(); i++) {
                User user1=userList.get(i);
                String returnString=String.format("%s,%s,%s,%s,%s,%s",user1.getId(),user1.getUserName(),user1.getPassWord(),user1.getQuestion(),user1.getAnswer(),user1.getCall());
                bufferedWriter.write(returnString);
                bufferedWriter.newLine();
            }
            bufferedReader.close();
            bufferedWriter.close();
            return true;
        }catch (IOException e){
            System.out.println("io异常，更改用户信息失败！");
            return false;
        }
    }

    /*
    根据id,查询用户
     */
    public User seekUserById(Integer id) {
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader("QQSystem\\User.txt"));
            List<User> userList = new ArrayList<>();
            String string;
            while ((string = bufferedReader.readLine()) != null) {
                String[] dates = string.split(",");
                User user = new User(Integer.parseInt(dates[0]), dates[1], dates[2], dates[3], dates[4], Integer.parseInt(dates[5]));
                userList.add(user);
            }
            for (int i = 0; i < userList.size(); i++) {
                User user = userList.get(i);
                if (user.getId().equals(id)) {
                    return user;
                }
            }
            bufferedReader.close();
        }
        catch (IOException e) {
            System.out.println("IO异常，查询失败！");
            return null;
        }
        return null;
    }
    /*
    查询所有用户信息
     */
    @Override
    public List<User> seekAllUser() {
        try {
            BufferedReader bufferedReader=new BufferedReader(new FileReader("QQSystem\\User.txt"));
            List<User> userList=new ArrayList<>();
            String string;
            while ((string=bufferedReader.readLine())!=null){
                String[]dates=string.split(",");
                User user=new User(Integer.parseInt(dates[0]),dates[1],dates[2],dates[3],dates[4],Integer.parseInt(dates[5]));
                userList.add(user);
            }
            bufferedReader.close();
            return userList;
        }
        catch (IOException e){
            System.out.println("io异常，查询用户信息失败！");
        }
        return null;
    }
    /*
    根据用户名查询用户
     */
    @Override
    public User seekUserByUserName(String userName) {
        try{
            BufferedReader bufferedReader=new BufferedReader(new FileReader("QQSystem\\User.txt"));
            List<User> userList=new ArrayList<>();
            String string;
            while ((string=bufferedReader.readLine())!=null){
                String[] dates=string.split(",");
                User user=new User(Integer.parseInt(dates[0]),dates[1],dates[2],dates[3],dates[4],Integer.parseInt(dates[5]));
                userList.add(user);
            }
            for (int i = 0; i < userList.size(); i++) {
                User user=userList.get(i);
                if (user.getUserName().equals(userName)){
                    return user;
                }
            }
            bufferedReader.close();
        }catch (IOException e) {
            System.out.println("io异常，查询用户信息失败！");
        }
        return null;
    }
}

